﻿"use strict";
define(["admin/app", "service/admin/tokenShopCategoryService", "ui.file.manage", "ui.sortable"], function (app) {
    app.controller("tokenShopCategoryController", function ($scope, $state, $stateParams, $location, utils, tokenShopCategoryService, w5cValidator) {
        $scope.psdisabled = false;
        $scope.query = {
            page: 1, size: 30, q: '', name: '', depth: $stateParams.depth
        };
        var methods = {
            search: function () {
                tokenShopCategoryService.gets($scope.query).success(function (response) {
                    $scope.model = response.data;
                });
            },
            search_page: function (page) {
                $scope.query.page = page;
                methods.search('page', $scope.query.page);
            },
            edit: function () {
                if ($stateParams.id > 0) {
                    tokenShopCategoryService.get({ id: $stateParams.id }).success(function (response) { $scope.model = response.data; });
                    $scope.breadcrumb = '编辑';
                }
                else {
                    tokenShopCategoryService.get({ id: 0 }).success(function (response) {
                        $scope.model = { id: 0, depth: $stateParams.depth, first_category_list: response.data.first_category_list, parent_id_value: response.data.parent_id_value };
                        $scope.breadcrumb = '新增';
                    });
                }
            },
            goBack: function () {
                history.back();
            },
            save: function () {
                var modal = utils.confirm({ msg: "确定要保存该分类么", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    var model = $scope.model;
                    $scope.psdisabled = true;
                    tokenShopCategoryService.save(model).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000)
                            methods.goBack();
                        else
                            $scope.psdisabled = false;
                    }).error(function () { $scope.psdisabled = false; });
                });
            },
            remove: function (model) {
                var modal = utils.confirm({ msg: "是否需要删除当前分类", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenShopCategoryService.delete(model.id).success(function (response) {
                        utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                        if (response.result_code == 1000) {
                            utils.remove($scope.model, model);
                            methods.search();
                        }
                    })
                })
            },
            updateStatus: function (id, status) {
                var tip = status == 0 ? "启用": "停用";
                var modal = utils.confirm({ msg: "是否需要" + tip + "该分类", ok: "确定", cancel: "取消" });
                modal.result.then(function () {
                    tokenShopCategoryService.updateStatus(id, status).success(function () {
                        methods.search();
                    });
                })
            }
        }
        angular.extend($scope, methods);
        if ($state.includes('app.tokenShopCategory.detail')) {
            methods.edit();
        } else {
            methods.search();
        }
    });
});